package com.mall.mymall.controller;


import com.mall.mymall.common.Result;
import com.mall.mymall.dao.dto.QueryDto;
import com.mall.mymall.dao.dto.querypage.RefundPageRequest;
import com.mall.mymall.dao.entity.Refunds;
import com.mall.mymall.service.OrdersService;
import com.mall.mymall.service.RefundsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 十里坡
 * @since 2025-03-24
 */
@RestController
@RequestMapping("/refunds")
public class RefundsController {


    @Autowired
    RefundsService refundsService;
    @Autowired
    OrdersService ordersService;

    @PostMapping("/pageQuery")
    public Result<QueryDto<Refunds>> pageQuery(@RequestBody RefundPageRequest refundPageRequest){
        QueryDto<Refunds> refundsQueryDto = refundsService.pageRefunds(refundPageRequest);
        return Result.success(refundsQueryDto);
    }

    @PutMapping("/auditRefunds/{refundsId}")
    public Result<String> auditRefunds(@RequestBody Refunds refunds, @PathVariable Integer refundsId){

        try {
            if(refunds.getAuditOpinion() == null){
                return Result.fail("审核意见不能为空");
            }
            if (refunds.getOrderStatus().equals("已处理")){
                ordersService.changeStatus("已退单",refunds.getOrderId());
            }
            refundsService.updateById(refunds);
            return Result.success("审核成功");
        } catch (Exception e) {
            return Result.fail("审核失败");
        }

    }

}

